package de.tabuma.projectview.navbar;

import java.util.*;
import java.util.regex.*;

import javax.servlet.http.*;
import de.tabuma.pm.project.entity.*;
import de.tabuma.projectview.*;
import de.tabuma.pm.externalcontent.entity.*;
import de.tabuma.pm.externalcontent.logik.*;
import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.pm.externalproject.logik.*;
import de.tabuma.pm.projectnavigation.entity.*;
import de.tabuma.pm.projectnavigation.logik.*;
import de.tabuma.ssoclient.entity.Attribute;


/**
 *
 * @author alefen
 */
public class JavaScriptFacade {

    public static String getPrintLoginWindow(ExternalProject ep) {
        String loginText = "Login for "+ep.getName();
        String login = "Login";
        int id = ep.getId().intValue();
        String str = "";
        List<LoginUserParameter> list = LoginUserParameterFacade.getList(ep.getId().intValue());

        str += "\t\tfunction "+constants.printLoginWindow + id + "("+constants.responseFunction+") {\n";
        str += "\t\t"+constants.showLoginWindow+id+" = 1;\n";
        str += "\t\t\tstr" + id + "='';\n";
        str += "\t\t\tstr" + id + "+='<p>"+loginText+"</p>';\n";
        str += "\t\t\tstr" + id + "+='<table>';\n";
        for(int i=0;i<list.size();i++) {
            LoginUserParameter lup = list.get(i);
            str += "\t\t\tstr" + id + "+='<tr>';\n";
            str += "\t\t\tstr" + id + "+='<td>" + lup.getNameForUser() + "</td>';\n";
            str += "\t\t\tstr" + id + "+='<td><input type=\"text\" name=\"" + constants.loginVarName + lup.getId() + "\" value=\"\"/></td>';\n";
            str += "\t\t\tstr" + id + "+='</tr>';\n";
        }
        str += "\t\t\tstr" + id + "+='</table>';\n";
        str += "\t\t\tstr" + id + "+='<input type=\"submit\" name=\"Login\" value=\""+login+"\" onClick=\"javascript:parent.'+"+constants.responseFunction+"+'()\">';\n";
        str += "\t\t\t" + constants.printIFrame+"(str"+id+");\n";
        str += "\t\t}\n";
        return str;
    }

    public static String getGeneralLoginFunction(ExternalProject ep) {
        int id = ep.getId().intValue();
        String str = "";
        List<LoginUserParameter> list = LoginUserParameterFacade.getList(ep.getId().intValue());
        str += "\t\tfunction " + constants.printGenerelLoginResponseFunction + id + "() {\n";
        str += "\t\t\tif("+constants.madeLoginInProject+ep.getId().intValue()+"==0) {\n";
        for(int i=0;i<list.size();i++) {
            LoginUserParameter lup = list.get(i);
            str += "\t\t\t\tdocument." + constants.projectForm + ep.getId() + ".elements[\"" + lup.getName() + "\"].value = parent."+constants.contentIframe+".document.getElementsByName(\"" + constants.loginVarName + lup.getId() + "\")[0].value;\n";
        }
        str += "\t\t\t\t" + constants.madeLoginInProject + ep.getId() + " = 1;\n";
        str += "\t\t\t}\n";
        str += "\t\t}\n";
        return str;
    }

    public static String getLoginFunction(ExternalContent ec, ExternalProject ep) {
        int id = ec.getId().intValue();

        String str = "\t\tfunction "+constants.printLoginResponseFunction+id+"() {\n";
        if(ec.getExternProjectId()!=0) {
            String url = ec.getUrl();
            str += "\t\t\t\tdocument.getElementById(\""+constants.contentIframe+"\").style.visibility = \"hidden\";\n";
            str += "\t\t\t\tdocument.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"hidden\";\n";
            str += "\t\t\tif("+constants.showLoginWindow+ep.getId()+"==0) {\n";
            str += "\t\t\t\tif("+constants.madeLoginInProject+ep.getId()+"==0) {\n";
            str += "\t\t\t\t\t"+constants.printLoginWindow+ep.getId()+"('"+constants.responseFunction+id+"');\n";
            str += "\t\t\t\t\treturn;\n";
            str += "\t\t\t\t}\n";
            str += "\t\t\t}\n";

            str += "\t\t\t"+constants.printGenerelLoginResponseFunction + ep.getId().intValue() +"();\n";
            str += "\t\t\tvar2 = new Date();\n";
            str += "\t\t\tvar1 = var2.getTime() - "+constants.varActionTime+ep.getId().intValue()+";\n";
            //is last action less than 5 Minutes ago?
            str += "\t\t\tif(var1 > 300000) {\n";
            //yes
            List<LoginUserParameter> lupList = LoginUserParameterFacade.getList(ep.getId().intValue());
            for(int i=0;i<lupList.size();i++) {
                LoginUserParameter lup = lupList.get(i);
                url = url.replaceAll(Pattern.quote("$"+lup.getName()), "'+document." + constants.projectForm + ep.getId() +"." + lup.getName()+ ".value+'");
            }
            str += "\t\t\t\tdocument."+constants.projectForm+ep.getId()+".submit();\n";
            str += "\t\t\t\tdocument.getElementById(\""+constants.loginWaitWindow+"\").style.visibility = \"visible\";\n";
            str += "\t\t\t\twindow.setTimeout(\"document.getElementsByName('"+constants.contentIframe1+"')[0].src='" + url + "';document.getElementById('"+constants.loginWaitWindow+"').style.visibility = 'hidden';\",5000);document.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"visible\";\n";
            str += "\t\t\t\tvar2 = new Date();\n";
            str += "\t\t\t\t"+constants.varActionTime + ep.getId().intValue() + " = var2.getTime();\n";
            str += "\t\t\t}\n";
            //no
            str += "\t\t\telse {\n";
            str += "\t\t\t\tvar2 = new Date();\n";
            str += "\t\t\t\t"+constants.varActionTime + ep.getId().intValue() + " = var2.getTime();\n";
            str += "\t\t\t\tdocument.getElementsByName('"+constants.contentIframe1+"')[0].src='" +  url + "';\n";
            str += "\t\t\t\tdocument.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"visible\";\n";
            str += "\t\t\t}\n";
        }
        else {
            str += "\t\t\t\tdocument.getElementsByName('"+constants.contentIframe1+"')[0].src='" +  ec.getUrl() + "';\n";
            str += "\t\t\t\tdocument.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"visible\";\n";
        }
        str += "\t\t}\n";
        return str;
    }

    public static String getExternalProjectVars(ExternalProject ep, List<Attribute> aList) {
        String str = "";

        List<LoginUserParameter> lupList = LoginUserParameterFacade.getList(ep.getId().intValue());
        boolean loginNeeded = false;
        for(int i=0;i<lupList.size();i++) {
            LoginUserParameter lup = lupList.get(i);
            String value = "";
            String name = ep.getName()+"/"+lup.getNameForUser()+" ("+lup.getId().intValue()+")";
            boolean foundAttribute = false;
            for(int j=0;j<aList.size();j++) {
                Attribute attribute = aList.get(j);
                if(attribute.getName().equals(name)) {
                    foundAttribute = true;
                    value = attribute.getValue();
                    j = aList.size();
                }
            }
            if(foundAttribute==false) {
                loginNeeded = true;
            }
            str += "\t\tvar "+constants.loginVarName+lup.getId().intValue()+" = '"+value+"';\n";
        }
        str += "\t\tvar "+constants.varActionTime+ep.getId()+" = 0;\n";

        if(loginNeeded) {
            str += "\t\tvar "+constants.showLoginWindow+ep.getId()+" = 0;\n";
            str += "\t\tvar "+constants.madeLoginInProject+ep.getId()+" = 0;\n";
        }
        else {
            str += "\t\tvar "+constants.showLoginWindow+ep.getId()+" = 1;\n";
            str += "\t\tvar "+constants.madeLoginInProject+ep.getId()+" = 1;\n";
        }
        return str;
    }

    public static String getProjectString(HttpServletRequest request, Project project, List<Attribute> aList) {
        String str = "";

        int projectId = project.getId().intValue();

        List<ExternalProject> epList = ExternalProjectFacade.getList(projectId);
        List<ProjectNavigation> pnList = ProjectNavigationFacade.getList(projectId);
        for(int i=0;i<epList.size();i++) {
            ExternalProject ep = epList.get(i);
            str += getExternalProjectVars(ep, aList);
            str += getGeneralLoginFunction(ep);
            str += getPrintLoginWindow(ep);
        }
        for(int i=0;i<pnList.size();i++) {
            ProjectNavigation pn = pnList.get(i);
            if(pn.getNavMode()==de.tabuma.pm.constants.ProjectNavigationExternalContent) {
                ExternalContent ec = ExternalContentFacade.getWithNavigationId(pn.getId().intValue());
                for(int j=0;j<epList.size();j++) {
                    ExternalProject ep = epList.get(j);
                    if(ep.getId().intValue()==ec.getExternProjectId()) {
                        str += getLoginFunction(ec, ep);
                    }
                }
                if(ec.getExternProjectId()==0) {
                    str += getLoginFunction(ec, null);
                }
            }
        }

        str += ContentFacade.getContent(request, projectId);

        str += "\t\tfunction init() {\n";
        str += "\t\t\tdocument.getElementById(\""+constants.contentIframe+"\").style.visibility = \"visible\";\n";
        str += "\t\t\tdocument.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"hidden\";\n";
        str += "\t\t\tdocument.getElementById(\""+constants.loginWaitWindow+"\").style.visibility = \"hidden\";\n";
        if(project.getStartPageId().intValue()!=0) {
            str += "\t\t\tparent."+constants.functionName+project.getStartPageId()+"();\n";
        }
        str += "\t\t}\n";

        return str;
    }
}
